/********************************************************************************
* Copyright (c) 2009, Matteo Pelati (matteo.pelati@gmail.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the <organization> nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Matteo Pelati ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**************************************************************************************/

#pragma once


#include <fstream>
#include <string>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include "dbxml/DbXml.hpp"
#include "dbXmlLib.h"
#include "DbEnvironment.h"
#include "DbXmlQueryContext.h"
#include "DbXmlDocument.h"
#include "DbXmlUpdateContext.h"
#include "DbXmlTransaction.h"
#include "DbXmlIndexSpecification.h"

using namespace DbXml;
using namespace System;
using namespace System::Collections::Generic;


namespace BerkeleyDB { namespace Xml
{

	public enum class DbXmlContainerType
	{
		NodeContainer = XmlContainer::NodeContainer,
		WholedocContainer = XmlContainer::WholedocContainer
	};

	[Flags] public enum class DbXmlContainerInitFlags : u_int32_t
	{
		Create = DB_CREATE,
		ReadUncommitted = DB_READ_UNCOMMITTED,
		Excl = DB_EXCL,
		Multiversion = DB_MULTIVERSION,
		NoMmap = DB_NOMMAP,
		ReadOnly = DB_RDONLY,
		ThreadFree = DB_THREAD,
		XACreate = DB_XA_CREATE,
		Chksum = DBXML_CHKSUM,
		Encrypt = DBXML_ENCRYPT,
		TxnNotDurable = DB_TXN_NOT_DURABLE,
		IndexNodes = DBXML_INDEX_NODES,
		NoIndexNodes = DBXML_NO_INDEX_NODES,
		Statistics = DBXML_STATISTICS,
		NoStatistics = DBXML_NO_STATISTICS,
		Transactional = DBXML_TRANSACTIONAL,
		AllowValidation = DBXML_ALLOW_VALIDATION
	};

	[Flags] public enum class DbXmlDocumentCreateFlags : u_int32_t
	{
		GenerateName = DBXML_GEN_NAME,
		WellFormedOnly = DBXML_WELL_FORMED_ONLY
	};

	public ref class DbXmlContainer : DbGenericObject
	{
	private:
		XmlContainer* n_cont;
	internal:
		XmlContainer* GetInnerObject()
		{
			return n_cont;
		}
		DbXmlContainer(DbGenericObject^ parent, XmlContainer& cont) : DbGenericObject(parent)
		{
			n_cont = new XmlContainer(cont);
		}
	public:
		~DbXmlContainer()
		{
			GC::SuppressFinalize(this);
			this->!DbXmlContainer();
		}
		!DbXmlContainer()
		{
			delete n_cont;
		}
	public:

		PROPERTY_RET_CSTRING_GET(Name,, Name);
		PROPERTY_RET_UINT32_GET(PageSize,, PageSize);
		PROPERTY_RET_ENUM_GET(Type,, ContainerType, DbXmlContainerType, XmlContainer::ContainerType);
		PROPERTY_RET_SIZE_GET(NumDocuments,, NumDocuments);
		PROPERTY_RET_ENUM_GET(Flags,, Flags, DbXmlContainerInitFlags, u_int32_t);
		PROPERTY_RET_BOOL_GET(IndexNodes,, IndexNodes);

		void Sync()
		{
			n_cont->sync();
		}

		DbXmlDocument^ GetDocument(String^ name, DbXmlDocumentFlags flags);
		DbXmlDocument^ GetDocument(DbXmlTransaction^ txn, String^ name, DbXmlDocumentFlags flags);
		void PutDocument(DbXmlDocument^ doc, DbXmlUpdateContext^ ctx, DbXmlDocumentCreateFlags flags);
		void PutDocument(DbXmlTransaction^ txn, DbXmlDocument^ doc, DbXmlUpdateContext^ ctx, DbXmlDocumentCreateFlags flags);
		void PutDocument(String^ name, String^ content, DbXmlUpdateContext^ ctx, DbXmlDocumentCreateFlags flags);
		void PutDocument(DbXmlTransaction^ txn, String^ name, String^ content, DbXmlUpdateContext^ ctx, DbXmlDocumentCreateFlags flags);
		void DeleteDocument(String^ name, DbXmlUpdateContext^ ctx);
		void DeleteDocument(DbXmlDocument^ doc, DbXmlUpdateContext^ ctx);
		void DeleteDocument(DbXmlTransaction^ txn, String^ name, DbXmlUpdateContext^ ctx);
		void DeleteDocument(DbXmlTransaction^ txn, DbXmlDocument^ doc, DbXmlUpdateContext^ ctx);
		void UpdateDocument(DbXmlDocument^ doc, DbXmlUpdateContext^ ctx);
		void UpdateDocument(DbXmlTransaction^ txn, DbXmlDocument^ doc, DbXmlUpdateContext^ ctx);
		void AddAlias(String^ name);
		bool RemoveAlias(String^ name);
		void AddIndex(String^ uri, String^ name, String^ index, DbXmlUpdateContext^ ctx);
		void AddIndex(DbXmlTransaction^ txn, String^ uri, String^ name, String^ index, DbXmlUpdateContext^ ctx);
		void AddIndex(String^ uri, String^ name, DbXmlIndexSpecificationFlags type, DbXmlValueType syntax, DbXmlUpdateContext^ ctx);
		void AddIndex(DbXmlTransaction^ txn, String^ uri, String^ name, DbXmlIndexSpecificationFlags type, DbXmlValueType syntax, DbXmlUpdateContext^ ctx);
		void AddDefaultIndex(String^ index, DbXmlUpdateContext^ ctx);
		void ReplaceDefaultIndex(DbXmlTransaction^ txn, String^ index, DbXmlUpdateContext^ ctx);
		void ReplaceDefaultIndex(String^ index, DbXmlUpdateContext^ ctx);
		void AddDefaultIndex(DbXmlTransaction^ txn, String^ index, DbXmlUpdateContext^ ctx);
		void DeleteIndex(String^ uri, String^ name, String^ index, DbXmlUpdateContext^ ctx);
		void DeleteIndex(DbXmlTransaction^ txn, String^ uri, String^ name, String^ index, DbXmlUpdateContext^ ctx);
		DbXmlDocumentCollection^ GetAllDocuments(DbXmlDocumentFlags flags);
		DbXmlDocumentCollection^ GetAllDocuments(DbXmlTransaction^ txn, DbXmlDocumentFlags flags);
		DbXmlIndexSpecification^ GetIndexSpecification();
		DbXmlIndexSpecification^ GetIndexSpecification(DbXmlTransaction^ txn, bool writeLock);
		DbXmlValue^ GetNode(String^ nodeHandle, DbXmlDocumentFlags flags);
		DbXmlValue^ GetNode(DbXmlTransaction^ txn, String^ nodeHandle, DbXmlDocumentFlags flags);
		void ReplaceIndex(String^ uri, String^ name, String^ index, DbXmlUpdateContext^ ctx);
		void ReplaceIndex(DbXmlTransaction^ txn, String^ uri, String^ name, String^ index, DbXmlUpdateContext^ ctx);
		void SetIndexSpecification(DbXmlIndexSpecification^ index, DbXmlUpdateContext^ ctx);
		void SetIndexSpecification(DbXmlTransaction^ txn, DbXmlIndexSpecification^ index, DbXmlUpdateContext^ ctx);




	


	};

}}